﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Threading;
using Microsoft.SharePoint;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;

namespace ISolutions.SharePoint.Mui.Controls
{
    public abstract class MuiControl : FormComponent
    {
        /// <summary>
        /// Name of the column to render. Make sure to create 
        /// </summary>
        public string FieldName { get; set; }

        public string LocalizedFieldName
        {
            get
            {
                return GenerateLocalizedFieldName(Thread.CurrentThread.CurrentUICulture.LCID.ToString());
            }
        }

        public string GenerateLocalizedFieldName(string language)
        {
            if (Thread.CurrentThread.CurrentUICulture.LCID == SPContext.Current.Web.Language)
            {
                return FieldName;
            }
            else
            {
                return string.Format("{0}_{1}", FieldName, language);
            }
        }

        public string GetLocalizedContent()
        {
            string content = string.Empty;
            if (SPContext.Current.ListItem.Fields.ContainsField(LocalizedFieldName))
            {
                content = TryGetFieldValue(LocalizedFieldName);
            }
            if (String.IsNullOrEmpty(content))
            {
                content = TryGetFieldValue(FieldName);
            }
            return content;
        }

        private string TryGetFieldValue(string fieldname)
        {
            if (SPContext.Current.ListItem[fieldname] != null)
            {
                return SPContext.Current.ListItem[fieldname].ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        protected void SaveValue(string languageID, string value)
        {

            SPContext.Current.ListItem[GenerateLocalizedFieldName(languageID)] = value;
        }

        #region Exception Handling
        List<Exception> exceptions = new List<Exception>();
        protected void RenderExceptions(HtmlTextWriter writer)
        {
            if (exceptions.Count > 0)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Table);
                foreach (Exception ex in exceptions)
                {
                    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
                    writer.RenderBeginTag(HtmlTextWriterTag.Td);
                    //   writer.WriteLine(ex.ToString());
                    writer.WriteLine(ex.Message);
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                }
                writer.RenderEndTag(); // table

                writer.EndRender();
            }
        }

        public void HandleException(Exception ex)
        {
            exceptions.Add(ex);
            if (ex.InnerException != null)
            {
                HandleException(ex.InnerException);
            }
        }
        #endregion
    }
}
